﻿mainModule.controller("tabularEditCtrl", ["$scope", "$rootScope", "itemSvc", "notification", "pageState", "columnSvc", "$routeParams", "cache", "articleSvc", "arrUtil", function ($scope, $rootScope, itemSvc, notification, pageState, columnSvc, $routeParams, cache, articleSvc, arrUtil) {
    var mode;
    var articleID = $routeParams.articleID;
    var article = {};    
    
    function hide() {
        $scope.isEditFormVisible = false;
    }
    function show() {
        $scope.isEditFormVisible = true;
    }    
    
    var columnsList;
    function getColumns() {
        return articleSvc.findById(articleID).then(function (response) {
            article = response.data;
            columnsList = response.data.Columns;
            arrUtil.cloneColumnInList(columnsList, "ID", "ColumnID");
        });
    }
    
    getColumns().then(function () {        
        if (article.TypeID === 1) {
            setAddMode();
            show();
        }
        else
            hide();
    });    
    
    function extendColumnsWithValues(valuesList) {
        var list = [], i, j, valueItem;
        for (i = 0; i < columnsList.length; i++) {
            valueItem = null;
            for (j = 0; j < valuesList.length; j++) {                
                if (columnsList[i].ID === valuesList[j].ColumnID)
                    valueItem = valuesList[j]; 
            }
            list.push(angular.extend({}, columnsList[i], valueItem));
        }
        return list;
    }

    $scope.$on('itemClicked', function (e, id) {
        getColumns().then(function () {
            mode = "edit";
            show();        
            itemSvc.findById(id).then(function (response) {
                $scope.item = response.data;
                $scope.item.DataItems = extendColumnsWithValues($scope.item.DataItems);                                
            });
        });
    });

    function setAddMode() {
        getColumns().then(function () {
            mode = "add";
            $scope.item = { ID: 0 };            
            arrUtil.deletePropInList(columnsList, "Value");
            $scope.item.DataItems = columnsList;
            show();
        });
    }

    $scope.$on('itemAdding', function () {
        setAddMode();
    });
    
    $scope.cancel = function () {
        hide();
    }

    $scope.save = function () {
        var item = $scope.item;
        item.ItemID = $scope.item.ID;
            
        if ($scope.editPanel.$valid) {
            if (mode === "add") {
                itemSvc.add(item).then(function (response) {
                    if (response.data.Success) {
                        item.ItemID = response.data.Value;
                        $scope.$emit('itemAdded', item);
                    }
                    notification.notify(response.data);
                    hide();
                });
            }
            else {
                itemSvc.edit($scope.item.ID, item).then(function (response) {
                    if (response.data.Success) {
                        $scope.$emit('itemEdited', item);
                    }
                    notification.notify(response.data);
                    hide();
                });
            }
        }
    }
}])